package com.hzncc.flowable_diboot.project_contracts.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.diboot.core.service.DictionaryService;
import com.diboot.core.util.BeanUtils;
import com.diboot.core.util.D;
import com.diboot.core.util.V;
import com.diboot.core.vo.JsonResult;
import com.diboot.core.vo.LabelValue;
import com.diboot.core.vo.Status;
import com.diboot.file.controller.BaseExcelFileController;
import com.diboot.file.excel.listener.ReadExcelListener;
import com.diboot.file.util.ExcelHelper;
import com.diboot.iam.annotation.BindPermission;
import com.diboot.iam.annotation.Log;
import com.diboot.iam.annotation.OperationCons;
import com.hzncc.flowable_diboot.project_contracts.dto.ProjectContractItemDTO;
import com.hzncc.flowable_diboot.project_contracts.entity.ProjectContractItem;
import com.hzncc.flowable_diboot.project_contracts.excel.listener.ProjectContractItemImportListener;
import com.hzncc.flowable_diboot.project_contracts.excel.model.ProjectContractItemExportModel;
import com.hzncc.flowable_diboot.project_contracts.excel.model.ProjectContractItemImportModel;
import com.hzncc.flowable_diboot.project_contracts.service.ProjectContractItemService;
import com.hzncc.flowable_diboot.project_contracts.vo.ProjectContractItemListVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import jakarta.servlet.http.HttpServletResponse;

import java.util.*;

/**
* 项目类合同合同项目 Excel处理Controller
*
* @author MyName
* @version 1.0
* @date 2024-09-27
 * Copyright © MyCorp
*/
@Slf4j
@RequestMapping("/project-contract-item/excel")
@RestController
@BindPermission(name = "项目类合同合同项目导入导出", code = "ProjectContractItemExcel")
public class ProjectContractItemExcelController extends BaseExcelFileController {

    @Autowired
    private ProjectContractItemService projectContractItemService;
    @Autowired
    private DictionaryService dictionaryService;
    /**
    * 预览数据
    *
    * @param file
    * @param params
    * @throws Exception
    */
    @Log(operation = "导入预览", businessObj = "ProjectContractItem")
    @BindPermission(name = "导入预览", code = OperationCons.CODE_IMPORT)
    @PostMapping("/preview")
    public JsonResult preview(@RequestParam("file") MultipartFile file, @RequestParam Map<String, Object> params) throws Exception {
        List<ProjectContractItemImportModel> dataList = (List<ProjectContractItemImportModel>) super.excelPreview(file, params).getData().get("dataList");
        List<LabelValue> paymentMethod = dictionaryService.getLabelValueList("paymentMethod");
        List<ProjectContractItemListVO> projectContractItemListVOS = BeanUtils.convertList(dataList, ProjectContractItemListVO.class);
        projectContractItemListVOS.forEach(item -> {
            item.setPaymentMethodLabel(paymentMethod.stream().filter(item1 -> Objects.equals(item1.getLabel(), item.getPaymentMethod())).findFirst().orElse(new LabelValue(item.getPaymentMethod(), "")));
            item.setPaymentMethod(paymentMethod.stream().filter(item1 -> Objects.equals(item1.getLabel(), item.getPaymentMethod())).findFirst().orElse(new LabelValue(item.getPaymentMethod(), "")).getValue().toString());
        });
        return JsonResult.OK(projectContractItemListVOS);
    }

    /**
    * 导入预览保存
    *
    * @param params
    * @throws Exception
    */
    @Log(operation = "导入预览保存", businessObj = "ProjectContractItem")
    @BindPermission(name = "导入预览保存", code = OperationCons.CODE_IMPORT)
    @PostMapping("/preview-save")
    public JsonResult previewSave(@RequestParam Map<String, Object> params) throws Exception {
        return super.excelPreviewSave(params);
    }

    /**
    * 上传excel并保存
    *
    * @param file
    * @param params
    * @throws Exception
    */
    @Log(operation = "直接导入", businessObj = "ProjectContractItem")
    @BindPermission(name = "直接导入", code = OperationCons.CODE_IMPORT)
    @PostMapping("/upload")
    public JsonResult upload(@RequestParam("file") MultipartFile file, @RequestParam Map<String, Object> params) throws Exception {
        return super.uploadExcelFile(file, params);
    }

    /**
    * 下载示例文件
    *
    * @param response
    * @throws Exception
    */
    @Log(operation = "导入项目类合同合同项目示例文件下载", businessObj = "ProjectContractItem")
    @BindPermission(name = "导入项目类合同合同项目示例文件下载", code = OperationCons.CODE_IMPORT)
    @GetMapping("/download-example")
    public void downloadExample(HttpServletResponse response) throws Exception{
        String fileName = "项目类合同合同项目导入示例.xlsx";
        ExcelHelper.exportExcel(response, fileName, ProjectContractItemImportModel.class, null);
    }

    /**
    * 项目类合同合同项目列表导出
    *
    * @param response
    * @return
    * @throws Exception
    */
    @Log(operation = "导出列表文件", businessObj = "ProjectContractItem")
    @BindPermission(name = "导出列表文件", code = OperationCons.CODE_EXPORT)
    @GetMapping("/export")
    public JsonResult export(ProjectContractItemDTO projectContractItemDto, @RequestParam(value = "columns", required = false) List<String> columns,
        HttpServletResponse response) throws Exception{
        QueryWrapper<ProjectContractItem> queryWrapper = super.buildQueryWrapperByQueryParams(projectContractItemDto);
        List<ProjectContractItemListVO> projectContractItemList = projectContractItemService.getViewObjectList(queryWrapper, null, ProjectContractItemListVO.class);
        if (V.isEmpty(projectContractItemList)) {
            return new JsonResult(Status.FAIL_OPERATION, "项目类合同合同项目列表为空，导出失败");
        }
        String fileName = "项目类合同合同项目列表导出_" + D.today() + ".xlsx";
        List<ProjectContractItemExportModel> dataList = this.entityList2ExcelList(projectContractItemList);
        ExcelHelper.exportExcel(response, fileName, ProjectContractItemExportModel.class, columns, dataList);
        return null;
    }
    /**
    * 获取表格导出头
    *
    * @return
    */
    @GetMapping("/export-table-head")
    public JsonResult getTableHeaderMap() {
        return new JsonResult<>(ExcelHelper.getTableHeads(ProjectContractItemExportModel.class));
    }

    /**
    * 实体列表转excel列表
    *
    * @param projectContractItemList
    * @return
    */
    private List<ProjectContractItemExportModel> entityList2ExcelList(List<ProjectContractItemListVO> projectContractItemList) {
        if (V.isEmpty(projectContractItemList)) {
            return Collections.emptyList();
        }
        return BeanUtils.convertList(projectContractItemList, ProjectContractItemExportModel.class);
    }

    @Override
    protected ReadExcelListener<ProjectContractItemImportModel> getExcelDataListener() {
        return new ProjectContractItemImportListener();
    }
}